/**
后台前端业务代码
 */
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import 'element-ui/lib/theme-chalk/display.css';

import router from './router/index';
import store from './store/index';
import customAxios from './api/interface';

import VueAxios from 'vue-axios'
import helper from './helper';
import Vue from "vue";
import ws from './ws';
// import constants from './constants'
import lodash, {isBoolean, isNumber} from "lodash";

// vxe table 大数据表格
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css'
import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx'
// vue router tab
import RouterTab from 'vue-router-tab'
import 'vue-router-tab/dist/lib/vue-router-tab.css'

VXETable.setup({
    size: "mini",
    zIndex: 2200,
    table: {
        showOverflow: 'tooltip',
        // showHeaderOverflow: 'title',
        stripe: true,
        border: true,
        autoResize: true,
        resizable: true,
        columnConfig: {
            isCurrent: true,
        },
        rowConfig: {
            isCurrent: true,
        },
        tooltipConfig: {
            enterable: true,
        }
    },
    toolbar: {
        export: {
            // type: ['csv', 'xlsx'],
        },
        tooltipConfig: {
            enterable: true,
        }
    }
});
VXETable.formats.mixin({
    // 转化成int
    formatInt({cellValue}) {
        return parseInt(cellValue) || 0;
    },
    // 保留2位小数
    formatToFixed({cellValue}, fixed = 2) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue) return 0;
        cellValue = fixed ? parseFloat(cellValue).toFixed(fixed) : Math.round(cellValue);
        return cellValue || 0;
    },
    // 人民币
    formatRmb({cellValue}, fixed = 0) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue) return 0;
        cellValue = fixed ? parseFloat(cellValue/100).toFixed(fixed) : Math.round(cellValue/100);
        return cellValue || 0;
    },
    // 百分比
    formatPercent({cellValue}, fixed = 1) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue) return 0;
        cellValue = (cellValue * 100).toFixed(fixed);
        return cellValue == 0 ? 0 : cellValue + '%';
    },
    // 时间
    formatDateTime({cellValue}) {
        if (isNaN(Number(cellValue))) return cellValue;
        return moment.unix(cellValue / 1000).format('YYYY-MM-DD HH:mm:ss')
    },
    // 渠道类型
    formatAppSourceType({cellValue}) {
        if (Array.isArray(cellValue)) {
            let label = '';
            cellValue.map(v => {
                label += (store.state.appSource.options.type.hasOwnProperty(v) ? store.state.appSource.options.type[v].label : v) + ','
            });
            return label.substr(0, label.length-1);
        }
        return store.state.appSource.options.type.hasOwnProperty(cellValue) ? store.state.appSource.options.type[cellValue].label : cellValue;
    },
    // 渠道名称
    formatAppSourceName({cellValue}) {
        if (isNaN(Number(cellValue))) return '-';
        cellValue = parseInt(cellValue);
        if (cellValue === 0) return '全部';
        let name = store.state.appSource.allName.hasOwnProperty(cellValue) ? store.state.appSource.allName[cellValue] : cellValue;
        return cellValue + ' ) ' + name;
    },
    // 场次名称
    formatSceneName({cellValue}) {
        if (!cellValue) return '-';
        let name = store.state.scene.allName.hasOwnProperty(cellValue) ? store.state.scene.allName[cellValue] : cellValue;
        return cellValue + ' ) ' + name;
    },
    // 千分位数字
    formatHuman({cellValue}, fixed = 0) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue || cellValue == 0) return 0;
        cellValue = fixed ? parseFloat(cellValue).toFixed(fixed) : Math.round(cellValue);
        return helper.numberFormat(cellValue);
    },
    // 千分位人民币
    formatHumanRmb({cellValue}, fixed = 0) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue) return 0;
        cellValue = fixed ? parseFloat(cellValue/100).toFixed(fixed) : Math.round(cellValue/100);
        return cellValue ? helper.numberFormat(cellValue) : 0;
    },
    // 格式化成万
    formatW({cellValue}, fixed = 0) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue) return 0;
        cellValue = fixed ? parseFloat(cellValue / 10000).toFixed(fixed) : Math.round(cellValue / 10000);
        return cellValue ? helper.numberFormat(cellValue) + 'w' : 0;
    },
    // 消耗转人民币
    formatCostRmb({cellValue}, fixed = 0) {
        if (isNaN(Number(cellValue))) return cellValue;
        if (!cellValue) return 0;
        let scale = window.currency_ticket_scale || 10;
        cellValue = fixed ? parseFloat(cellValue/scale).toFixed(fixed) : Math.round(cellValue/scale);
        return cellValue ? helper.numberFormat(cellValue) : 0;
    },
    // 布尔值转成中文是否
    formatBoolText({cellValue}, yesText = '是', noText = '否') {
        if (isBoolean(cellValue) || isNumber(cellValue)) return cellValue ? yesText : noText;
    },
    formatLevel({cellValue}) {
      if (!cellValue) return 'Lv.0';
        return 'Lv.' + cellValue;
    },
    // 道具数据改成文本输出  道具1: 数量1|道具2:数量2
    formatItemsToText({cellValue}) {
        if (!cellValue) return cellValue;
        if (!Array.isArray(cellValue)) {
            cellValue = [cellValue];
        }
        let text = "";

        cellValue.map(v => {
            let name = store.state.item.allName.hasOwnProperty(v.id) ? store.state.item.allName[v.id] : v.id;
            let num = helper.numberFormat(v.num);
            text += ` | ${name}: ${num}`;
        });
        return text.substr(3);
    },
    // json转成字符串输出
    formatJsonToString({cellValue}) {
        if (typeof(cellValue) === "string") return cellValue;
        return JSON.stringify(cellValue);
    },
    formaterQuick({cellValue}, data) {
        let label = '';
        data.map(v => {
            if (v.value === cellValue) {
                label = v.label;
                // label = v.class ? [`<el-tag type="${v.class}">${v.label}</el-tag>`] : v.label;
            }
        });
        return label;
    },
});
VXETable.use(VXETablePluginExportXLSX)
Vue.use(VXETable)

Vue.use(RouterTab)

require('./bootstrap');

window.Vue = require('vue');
// window.constants = constants;

// 放入 externals 中, 这里无需use
Vue.use(ElementUI);

// 全局配置
Vue.prototype.$ELEMENT = { size: 'mini'};

Vue.use(VueAxios, customAxios);

Vue.prototype.$helper = helper;
Vue.prototype.$admin = window.admin;
Vue.prototype.$ENABLE_EXPORT = window.admin.enable_export;// 导出
Vue.prototype.$ENABLE_CUSTOMER = window.admin.enable_customer; // 客服
Vue.prototype.$ENABLE_SOCKET = window.admin.enable_socket; // socket
Vue.prototype.$CURRENCY_BULLET_SCALE = window.currency_bullet_scale;// 奶酪比例
Vue.prototype.$CURRENCY_HB_SCALE = window.currency_hb_scale;// 红包比例
Vue.prototype.$CURRENCY_TICKET_SCALE = window.currency_ticket_scale;// 点券比例
Vue.prototype.$CDN_SITE_URL = window.cdn_site_url;// CDN 站点地址
Vue.prototype.$API_GO_URL = window.api_go_url;// API_GO 站点地址

Vue.prototype.$ws = ws;
Vue.prototype._ = lodash;

// 通用组件
Vue.component('boolean-tag', require('./components/Boolean2Tag').default);
Vue.component('boolean-icon', require('./components/Boolean2Icon').default);
Vue.component('destroy-confirm', require('./components/DestroyConfirm').default);
Vue.component('destroy-multiple', require('./components/DestroyMultiple').default);

// 常用picker
Vue.component('picker-datetime-range', require('./components/Picker/datetime-range').default);
Vue.component('picker-date-range', require('./components/Picker/date-range').default);

// 常用选择
// Vue.component('select-apps', require('./components/Select/apps').default);
Vue.component('select-app-source', require('./components/Select/app-source').default);
Vue.component('select-os', require('./components/Select/os').default);
Vue.component('select-item', require('./components/Select/item').default);
Vue.component('select-quick', require('./components/Select/quick').default);

// 常用标签
Vue.component('tag-app-source', require('./components/Tag/app-source').default);
Vue.component('tag-app-source', require('./components/Tag/app-source').default);
// Vue.component('tag-apps', require('./components/Tag/apps').default);
// Vue.component('tag-scenes', require('./components/Tag/scenes').default);
// 复制列
Vue.component('clipboard-column', require('./components/Dialog/clipboard-column').default);
// 用户信息
Vue.component('user-info', require('./components/UserInfo').default);

const app = new Vue({
    el: '#app',
    store,
    router,
});
